'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var serve = require('rollup-plugin-serve');
var livereload = require('rollup-plugin-livereload');
var resolve = require('@rollup/plugin-node-resolve');
var commonjs = require('@rollup/plugin-commonjs');
var cleaner = require('rollup-plugin-cleaner');
var typescript = require('@rollup/plugin-typescript');
var builtins = require('rollup-plugin-node-builtins');
var autoprefixer = require('autoprefixer');
var postcss = require('rollup-plugin-postcss');
var replace = require('@rollup/plugin-replace');
var html = require('@rollup/plugin-html');
var fs = require('fs');

const template =
    (globals) =>
    ({ bundle }) => {
        const scripts = Object.keys(bundle)
            .filter((name) => name?.endsWith('js'))
            .map((name) => `<script type="text/javascript" src="${name}"></script>`)
            .join('');

        const styles = Object.keys(bundle)
            .filter((name) => name?.endsWith('css'))
            .map((name) => `<link href="${name}" rel="stylesheet" type="text/css">`)
            .join('');

        return `<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>flame-chart-js</title>
    ${styles}
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
Object.assign(window, ${JSON.stringify(globals)});
</script>
${scripts}
</body>
</html>`;
    };

const pkg = JSON.parse(fs.readFileSync('./package.json').toString());

const defaultOptions = { env: 'production' };

function generate(compilerOptions = {}) {
    const options = {
        ...defaultOptions,
        ...compilerOptions,
    };

    console.log('Environment:', options.env);

    return {
        input: './example/src/index.tsx',
        output: {
            dir: './example/dist',
            entryFileNames: 'main-[hash].js',
            format: 'iife',
            name: 'bundle',
            sourcemap: 'inline',
        },
        plugins: [
            typescript({
                compilerOptions: { outDir: './example/dist' },
                include: ['./example/src/**/*.{ts,tsx}', './src/**/*.{ts,tsx}'],
            }),
            resolve({
                browser: true,
                preferBuiltins: true,
            }),
            postcss({
                plugins: [autoprefixer()],
                modules: true,
                sourceMap: true,
                extract: true,
                minimize: true,
            }),
            html({
                template: template({
                    app: {
                        version: pkg.version,
                    },
                }),
            }),
            commonjs(),
            builtins(),
            cleaner({
                targets: ['./example/dist'],
            }),
            replace({
                preventAssignment: true,
                'process.env.NODE_ENV': JSON.stringify(options.env),
            }),
        ],
    };
}

var rollup_config = generate();

const config = generate({ env: 'development' });

var rollup_example_dev_config = {
    ...config,
    plugins: [...config.plugins, serve({ contentBase: './example/dist' }), livereload({ verbose: true, delay: 1000 })],
};

exports.default = rollup_example_dev_config;
